<?php
require_once 'classes/photos.php';
require_once 'classes/albums.php';
require_once 'api/wikimapia.php';
require_once 'classes/mySession.php';

if(!$pgSession->isUserLoggedIn()){
	$retorno = array("status"=>FALSE,"message"=>"No user logged in.");
}else{
	$ownerN = $pgSession->getUserId();
	$idN = $_GET["photoid"];
	$lat = $_GET["latitude"];
	$lng = $_GET["longitude"];
	$propagationIds = $_GET["propagation"];
	$ids = array();
	$ids[0] = $idN;
	$stopEcho = false;
	if(isset($idN) && !is_null($idN)
			&& isset($lat) && !is_null($lat)
			&& isset($lng) && !is_null($lng)){
		$wikiMapiaApi = new WikimapiaAPI();
		$wikiObj = $wikiMapiaApi->getObjectsInPoint($lat,$lng);
		$geoAlbuns = WikimapiaAPI::getGeoAlbuns($wikiObj);
		
		$resGeoAlb = $albumUtils->saveGeoAlbuns($geoAlbuns);
		
		if(isset($propagationIds) && !is_null($propagationIds)){
			$ids = split(",",$propagationIds);
		}
		
		
		$resultPhotos = $photoUtils->savePhotoInGeoAlbuns($ids,$geoAlbuns);
		
		foreach($ids as $idProp){
			$p = new Photo($idProp, $ownerN, NULL, NULL, NULL);
			$p->setLatitudeDecimal($lat);
			$p->setLongitudeDecimal($lng);
				
			$resp = $photoUtils->savePhoto($p);
			if($idProp == $idN && !$stopEcho)
				echo json_encode($resp);
		}
		
	}
}

#CHECK SESSION TO USER ALREADY LOGGED IN

#IF NOT, REDIRECT TO LOGIN PAGE

#IF YES:
#	CHECK IF LAT AND LONG ARE VALID
#	IF YES:
#		CONNECT TO DATABASE
#		CHECK IF PHOTO ID IS VALID
#		TRY TO SAVE ON EXIF
#		TRY TO SAVE IN DATABASE
#		IF SAVED IN DATABASE, RETURN JSON WITH MESSAGE AND LOCATION
#	IF NOT, RETURN JSON WITH MESSAGE

?>